package com.xk.meeting.service;

import com.xk.meeting.dao.ApplyDao;
import com.xk.meeting.dao.ApplyDaoImpl;
import com.xk.meeting.entity.Apply;
import com.xk.meeting.entityutils.ListPage;
import com.xk.meeting.entityutils.PageBean;
import com.xk.meeting.entity.Regform;
import com.xk.meeting.entityutils.ApplyList;
import com.xk.meeting.entityutils.DateTime;

import java.text.SimpleDateFormat;
import java.util.*;

public class ApplyServiceImpl implements ApplyService{
    private ApplyDao applyDao = new ApplyDaoImpl();
    @Override
    public int addApply(Apply apply) {
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();

        startDate.setTime(apply.getStarttime());
        endDate.setTime(apply.getEndtime());

        if (startDate.get(Calendar.MINUTE) < 30) {
            startDate.set(Calendar.MINUTE, 0);
            apply.setStarttime(startDate.getTime());
        } else {
            startDate.set(Calendar.MINUTE, 30);
            apply.setStarttime(startDate.getTime());
        }

        if (endDate.get(Calendar.MINUTE) > 0 && endDate.get(Calendar.MINUTE) < 30) {
            endDate.set(Calendar.MINUTE, 30);
            apply.setEndtime(endDate.getTime());
        } else {
            endDate.add(Calendar.HOUR_OF_DAY,1);
            endDate.set(Calendar.MINUTE, 0);
            apply.setEndtime(endDate.getTime());
        }
        return applyDao.addApply(apply);
    }

    /*@Override
    public List<ApplyList> getApplyList() {
        List<ApplyList> lists = new ArrayList<>();
        ApplyList applyList = null;
        List<Apply> applys = applyDao.getApplys();
        MeetingService meetingService = new MeetingServiceImpl();
        RegformService regformService = new RegformServiceImpl();
        Regform regform = null;
        for (Apply apply : applys){

            applyList = new ApplyList();

            int num =  applyDao.getStatus(apply.getId());

            String status = null;
            String style = null;

            regform = regformService.getRegform(apply.getUid());
            switch (num){
                case -1 : {
                    status = "未通过";
                    style = "status-refuse";
                    break;
                }
                case 0 : {
                    status = "未审核";
                    style = "status-notyet";
                    break;
                }
                case 1 : {
                    status = "以通过";
                    style = "status-pass";
                    break;
                }
            }
            String name = meetingService.getName(apply.getMid());

            applyList.setRegform(regform);
            applyList.setName(name);
            applyList.setApply(apply);
            applyList.setStatus(status);
            applyList.setStyle(style);
            lists.add(applyList);
        }
        return lists;
    }*/

    /*@Override
    public List<ApplyList> getApplyList() {
        List<ApplyList> lists = new ArrayList<>();
        List<Integer> ids = applyDao.getApplyId();
        for (int id : ids){
            ApplyList applyList = getApplyList(id);
            lists.add(applyList);
        }
        return lists;
    }*/

    @Override
    public ListPage<ApplyList> getApplyList(int currentPage, int rows) {
        ListPage<ApplyList> listPage = new ListPage<>();
        int index = (currentPage-1)*rows;
        List<ApplyList> lists = new ArrayList<>();
        List<Integer> ids = applyDao.getApplys(index,rows);
        for (int id : ids){
            ApplyList applyList = getApplyList(id);
            lists.add(applyList);
        }
        int count = applyDao.getCount();

        listPage.setTotalCount(count);
        listPage.setTotalPage(getTotalPage(rows));
        listPage.setList(lists);
        listPage.setCurrentPage(currentPage);
        listPage.setRows(rows);

        return listPage;
    }


    @Override
    public ApplyList getApplyList(int id) {
        ApplyList applyList = null;
        Apply apply = applyDao.getApply(id);
        MeetingService meetingService = new MeetingServiceImpl();
        RegformService regformService = new RegformServiceImpl();
        Regform regform = null;
        applyList = new ApplyList();
        int num =  applyDao.getStatus(apply.getId());
        String status = null;
        String style = null;
        regform = regformService.getRegform(apply.getUid());
        switch (num){
            case -1 : {
                status = "未通过";
                style = "status-refuse";
                break;
            }
            case 0 : {
                status = "未审核";
                style = "status-notyet";
                break;
            }
            case 1 : {
                status = "以通过";
                style = "status-pass";
                break;
            }
        }
        String name = meetingService.getName(apply.getMid());

        applyList.setRegform(regform);
        applyList.setName(name);
        applyList.setApply(apply);
        applyList.setStatus(status);
        applyList.setStyle(style);
        return applyList;
    }

    @Override
    public int changeStatus(int id, int status) {
        return applyDao.changeStatus(id,status);
    }

    @Override
    public boolean isExistTime(String start, String end, String mid) {
        int count = applyDao.isExistTime(start,end,mid);
        if (count > 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Map<String, PageBean> getPageBean(int mid, DateTime datetime) {
        Map<String,PageBean> map = new HashMap<>();
        List<Apply> applys = applyDao.getApplys(mid,datetime);
        PageBean pageBean = null;
        for (Apply apply : applys){
            pageBean = new PageBean();
            pageBean.setTitle(apply.getTitle());

            Date starttime = apply.getStarttime();
            Date endtime = apply.getEndtime();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
            SimpleDateFormat sdf3 = new SimpleDateFormat("HH");
            SimpleDateFormat sdf4 = new SimpleDateFormat("mm");

            pageBean.setTime(sdf2.format(starttime)+"-"+sdf2.format(endtime));
            int hour = Integer.parseInt(sdf3.format(endtime)) - Integer.parseInt(sdf3.format(starttime));
            int second = Integer.parseInt(sdf4.format(endtime)) > 0 ? 1 : 0;
            pageBean.setHeight(hour*2+second);
            int status = apply.getStatus();
            switch (status){
                case 1: pageBean.setStyle("status-pass");break;
                case 0: pageBean.setStyle("status-notyet");
            }
            map.put(sdf.format(starttime),pageBean);
        }
        return map;
    }

    @Override
    public int getcount() {
        return applyDao.getCount();
    }

    @Override
    public int getUid(int id) {
        return applyDao.getUid(id);
    }

    @Override
    public String getMeetingName(int id) {
        MeetingService meetingService = new MeetingServiceImpl();
        int mid = applyDao.getMid(id);
        return meetingService.getName(mid);
    }

    @Override
    public String getDateTime(int id) {
        Date start = applyDao.getStart(id);
        Date end = applyDao.getEnd(id);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日HH时mm分到");
        SimpleDateFormat sdf2 = new SimpleDateFormat("HH时mm分");
        String strattime = sdf1.format(start);
        String endtime = sdf2.format(end);
        return strattime+endtime;
    }

    @Override
    public int getTotalPage(int rows) {
        int count = applyDao.getCount();
        return count % rows == 0 ? count / rows : count / rows + 1;
    }
}
